1. /* scfcsqrt.cpp by K.Tsuru */
  2. // function ID = 9101
  3. /**************************************************
  4. SComplex class
  5. It provides the sqrt(z) = x + i*y.
  6. It is assumed that -pi <= arg(z) <= pi, then x >= 0
  7. and y has the same sign as the imaginary part of z.
  8. I referred to gcc's "complext.cc".
  9. --------------------------------------------------
  10. z = a +i * b
  11. = (r, theta) ... Representation in polar coodinate.
  12. sqrt(z) = (sqrt(r), theta/2) = x+i*y
  13. Let theta = t.
  14. a = r cos(t), b = r sint.
  15. cos(t/2) = sqrt{ [1+cos(t)] / 2} =sqrt{[1 + (a/r)] / 2}
  16. then we get
  17. x = sqrt(r)cos(t/2) = sqrt{(r+a)/2} > 0 (always positive).
  18. In the same way
  19. y = [+|-]sqrt(r)sin(t/2) = [+|-]sqrt{(r-a)/2}
  20. (x+iy)^2=x^2-y^2+2ixy=a+ib
  21. x^2-y^2=a, 2xy=b
  22. x>0 then y has the same sign of b.
  23. **************************************************/
  24. #ifndef SN_H
  25. #include "sn.h"
  26. #endif
  27. SComplex Csqrt(const SComplex& z){
  28. if(z.Imag().Sign() == 0) { // b = 0, contains the case of a = 0 ver. 2.18
  29. SDouble r = Sqrt( Dabs( z.Real() ) );
  30. if(z.Real().Sign() > 0) return SComplex(r, 0.0);
  31. return SComplex(0.0, r);
  32. }
  33. SDouble r = Cabs(z), x, y, a = z.Real(), b = z.Imag(); // z = a + i b
  34. if(a.Sign() > 0) { // a > 0, -pi<theta<pi
  35. SDouble rx = RecSqrt( DsDiv(r + a, 2) ); // rx = sqrt{2/(r+a)} = 1/x
  36. x = DReciprocal(rx); // x = 1/rx = sqrt{(r+a)/2} > 0
  37. y = DsDiv(b, 2) * rx; // y = (b/2)*(1/x) = b/(2*x) the same sign of b
  38. } else { // a <= 0, r - a > 0, pi<theta<2*pi
  39. SDouble ry = RecSqrt( DsDiv(r - a, 2) ); // ry = sqrt{2/(r-a)} = 1/y
  40. y = DReciprocal(ry); // y = 1/ry = sqrt{(r-a)/2}
  41. x = DsDiv(b, 2) * ry; // x = (b/2)*(1/y) = b/(2*y)
  42. if(b.Sign() < 0){
  43. x.ChangeSign(); y.ChangeSign(); // x = -x = |x| y = -y <0 --> xy=b/2 < 0
  44. }
  45. }
  46. return SComplex(x, y);
  47. }

scfcsqrt.cpp : last modifiled at 2015/08/09 15:55:05(1,816 bytes)
created at 2017/10/06 15:21:28
The creation time of this html file is 2017/10/06 15:27:09 (Fri Oct 06 15:27:09 2017).